
package com.ywjg.serviceimp;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.domain.Userinfos;
import com.taobao.api.request.OpenimUsersAddRequest;
import com.taobao.api.request.OpenimUsersGetRequest;
import com.taobao.api.response.OpenimUsersAddResponse;
import com.taobao.api.response.OpenimUsersGetResponse;
import com.ywjg.dao.AppUserDao;
import com.ywjg.model.AppUser;
import com.ywjg.service.IAppUserService;
import com.ywjg.utils.Utils;
@Service
public class AppUserService implements IAppUserService {
	@Resource
	AppUserDao appUserDao;
	
     /* (non-Javadoc)
	 * @see com.ywjg.serviceimp.IAppUserService#findAppUserById(int)
	 */
	public AppUser findAppUserById(int id) {
		return appUserDao.findOne(id);
		
	}
	
	
	/* (non-Javadoc)
	 * @see com.ywjg.serviceimp.IAppUserService#updateAppUser(com.ywjg.model.AppUser)
	 */
	@Transactional
	public AppUser updateAppUser(AppUser u) {
		AppUser appUser  = appUserDao.findOne(u.getId());
		Utils.copyPropertiesIgnoreNull(u,appUser);
		return appUserDao.save(appUser);
	}
	
    /* (non-Javadoc)
	 * @see com.ywjg.serviceimp.IAppUserService#findAppUserByUserAccount(java.lang.String)
	 */
	public AppUser findAppUserByUserAccount(String userName) {
		
		return appUserDao.findAppUserByUserAccount(userName);
	}
	
	/* (non-Javadoc)
	 * @see com.ywjg.serviceimp.IAppUserService#addAppUser(com.ywjg.model.AppUser)
	 */
	@Transactional
	public AppUser addAppUser(AppUser appUser) {
		AppUser findAppUserByUserName = findAppUserByUserAccount(appUser.getUserAccount());
		if (null!=findAppUserByUserName) {
			return null;
		}
		return appUserDao.save(appUser);
	}


	@Async
	public void setAliMsgAccount(AppUser appUser) throws ApiException {
		TaobaoClient client = new DefaultTaobaoClient("http://gw.api.taobao.com/router/rest","23582882","9b75ea8e9406651baf17d768192e5b89");
		OpenimUsersAddRequest req = new OpenimUsersAddRequest();
		List<Userinfos> list = new ArrayList<Userinfos>();
		Userinfos userInfo = new Userinfos();
		userInfo.setUserid(appUser.getUserAccount());
		userInfo.setPassword(appUser.getUserAccount());
		list.add(userInfo);
		req.setUserinfos(list);
		OpenimUsersAddResponse rsp = client.execute(req);
	
		System.out.println(rsp.getBody());
		
	}
	
	
	
	
	
	@Transactional
	@Async
	public void setAppUserPoint(AppUser appUser) {
		String adress = appUser.getUserProvince()+appUser.getUserCity()
		+appUser.getUserDistrict()+
		appUser.getUserAddress();
		String userPoint = Utils.getPoInteger(adress);
		appUser.setUserPoint(userPoint);
		AppUser findOne = appUserDao.findOne(appUser.getId());
		Utils.copyPropertiesIgnoreNull(appUser,findOne);
		AppUser save = appUserDao.save(findOne);
		
		
	}
	
	
	
	
	
	
	
	
	
	



}